﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class AksesuarSeriTip2 : XForm
    {
        int AksSeriId;
        public AksesuarSeriTip2(int aksSeriId)
        {
            InitializeComponent();
            AksSeriId = aksSeriId;
            this.Text += "- " + AksSeriId.ToString();
        }

        DataTable dtDag;
        BindingSource bs;
        private void AksesuarSeriTip2_Load(object sender, EventArgs e)
        {

            dtDag = ExecuteDataTable("GetDagilimVertical");

            bs = gvAna.Init("GetAksSeri", AksSeriId);
            gvAna.AddDetailGrid(gvDetay, "DagilimId");
            gvDetay.MakeColumnInvisible("DagilimId");
            gvDetay.AddSummary("1", "2", "3");

            gvDetay.ColumnPanelRowHeight = 28;
            gvDetay.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            //gvDetay.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;

            gvDetay.Columns["1"].Visible = gvDetay.Columns["2"].Visible = gvDetay.Columns["3"].Visible = false;

            foreach (DataRow dr in gvAna.Dataset().Tables[2].Rows)
            {
                gvDetay.Columns[dr["idx"].ToString()].VisibleIndex = 1000 + (int)dr["idx"];

                gvDetay.SetCaptions(dr["idx"].ToString() + ":" +
                    AppSession.Lookups.V_Aksesuar.FindByAksesuarId((int)dr["AksesuarId"]).StokKod + "\n" +
                    AppSession.Lookups.V_Aksesuar.FindByAksesuarId((int)dr["AksesuarId"]).Boy);
            }

            gvAna.MakeReadOnly();
            gleTumu.Edit.Assign(RI(Rp.Dagilim));
            gleTumu.EditValueChanged += gleTumu_EditValueChanged;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            gvAna.AddSummary();


            //DataTable dt = AppSession.Lookups.Dagilim.Select("DagilimId in ("+
            //    gvAna.Dataset().Tables[1].Rows[0][0].ToString() + ")").CopyToDataTable();
            //RepositoryItemGridLookUpEdit riMevcut = GetCustomGLE(dt, "Name", "DagilimId");

            //gleMevcut.Properties.Assign(riMevcut);
            //gleMevcut.EditValueChanged += gleMevcut_EditValueChanged;
            //gleMevcut.EditValue = gvAna.Dataset().Tables[2].Rows[0][0];
            riDag.TextEditStyle = TextEditStyles.DisableTextEditor;
            bs.CurrentChanged += bs_CurrentChanged;
            bs_CurrentChanged(null, null);


            gvAna.BestFitColumns();
            gvDetay.BestFitColumns();

            GridManager gm = GridManager.GetManager(gvAna);
            gm.GridMenu.AddMenu("Bu DagilimId'ye bağlı kayıtları sil", DagilimIdSil);


        }

        void gleTumu_EditValueChanged(object sender, EventArgs e)
        {
            gvAna.RefreshDataSource("AddAksSeri", AksSeriId, intParse(gleTumu.EditValue));
            gvAna.FocusedRowHandle = gvAna.LocateByValue("DagilimId", intParse(gleTumu.EditValue));

            gvAna.BestFitColumns();
            gvDetay.BestFitColumns();
        }

        private void DagilimIdSil(object sender, EventArgs e)
        {
            gvAna.RefreshDataSource(true, "DelAksSeri", AksSeriId, intParse(bs, "DagilimId"));
        }

        void gvDetay_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
            {
                string boy = view.GetRowCellValue(e.RowHandle, "Boy").ToString();
                string dboy = view.GetRowCellValue(e.RowHandle, "DBoy").ToString();

                if (boy == dboy)
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
            }
        }

        void bs_CurrentChanged(object sender, EventArgs e)
        {
            riDag.Items.Clear();
            if (strParse(bs, "DagilimId") != string.Empty)
                foreach (DataRow dr in dtDag.Select("DagilimId=" + strParse(bs, "DagilimId")))
                    riDag.Items.Add(dr["Beden"].ToString());
        }


        RepositoryItemComboBox riDag = new RepositoryItemComboBox();

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAna.EndEditing();
            gvDetay.EndEditing();
            DataSet d = gvAna.Dataset();

            DataTable dt = ExecuteDataTable("UpdAksSeri2", 2, AksSeriId, d.Tables[1].Serialize(), d.Tables[2].Serialize());
            if (dt.Rows.Count > 0)
                //    XtraMessageBox.Show("Başarıyla Güncellendi");
                //else
                XtraMessageBox.Show(dt.Rows[0][0].ToString(), "Dikkat!");

            //gvDetay.Update("DBoy", "AksSeriLines");

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

    }
}
